<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of admins
 *
 * @author Maulnick
 */
require_once 'document_entity.php';
class PIB_entity extends Document_entity{
    public function load($id){
        // retrieve from database
        $result = array();
        $result['data'] = $this->get_pib($id);
        $lineitems = $this->get_pib_lineitem($result['data']->pib_id);
        $container_details = array();
        foreach($lineitems as $lineitem){
            $container_detail = array();
            $container_detail = $lineitem;
            $container_detail->ContainerDetail = $this->get_pib_containerdetail($lineitem->pib_lineitem_id);
            array_push($container_details, $container_detail);
        }
        $result['lineitem'] = $container_details;
        $this->set_data($result);
        return $this->get_data();
    }
    public function save(){
        // insert to database
        $data = $this->get_data();
        $pib = $data['data'];
        // 1. insert pib
        $step_1 = $this->db->insert('pib',$pib);
        if($step_1){
            $this->document_id = $this->get_last_id();
            foreach($data['lineitem'] as $container){
                // 2. insert pib_lineitem
                $container['pib_id'] = $this->document_id;
                $container_details = $container['ContainerDetail'];
                unset($container['ContainerDetail']);
                $step_2 = $this->db->insert('pib_lineitem', $container);
                if($step_2){
                    $id_container = $this->get_last_id_container();
                    foreach($container_details as $detail){
                        // 3. insert pib_lineitem_containerdetail
                        $detail['pib_lineitem_id'] = $id_container;
                        $step_3 = $this->db->insert('pib_lineitem_containerdetail', $detail);
                    }
                }
            }
        }
        return $step_1 && $step_2 && $step_3;
    }
    
    private function get_last_id(){
        //get last index pib
        $this->db->select('pib_id');
        $this->db->order_by('pib_id', 'DESC');
        $this->db->limit(1);
        $result = $this->db->get('pib');
        if($result->num_rows==1){
            return $result->row(0)->pib_id;
        }else{
            return false;
        }
    }
    private function get_last_id_container(){
        //get last index pib
        $this->db->select('pib_lineitem_id');
        $this->db->order_by('pib_lineitem_id', 'DESC');
        $this->db->limit(1);
        $result = $this->db->get('pib_lineitem');
        if($result->num_rows==1){
            return $result->row(0)->pib_lineitem_id;
        }else{
            return false;
        }
    }
    private function get_pib($pib_id){
        // get pib
        $this->db->where('pib_id',$pib_id);
        $result = $this->db->get('pib');
        if($result->num_rows==1){
            return $result->row(0);
        }else{
            return false;
        }
    }
    private function get_pib_lineitem($pib_id){
        // get pib_lineitem
        $this->db->where('pib_id',$pib_id);
        $result = $this->db->get('pib_lineitem');
        if($result->num_rows>0){
            return $result->result();
        }else{
            return false;
        }
    }
    private function get_pib_containerdetail($pib_lineitem_id){
        // get pib_lineitem_containerdetail
        $this->db->where('pib_lineitem_id',$pib_lineitem_id);
        $result = $this->db->get('pib_lineitem_containerdetail');
        if($result->num_rows>0){
            return $result->result();
        }else{
            return false;
        }
    }
    
    public function update_approvement($pib_id,$approvement){
        $data = array(
               'ApprovementStatus' => $approvement
            );
        $this->db->where('pib_id', $pib_id);
        return $this->db->update('pib', $data); 
    }
}